package com.cyh.mywebsocket.zhangke;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.cyh.mywebsocket.R;
import com.zhangke.websocket.SimpleListener;
import com.zhangke.websocket.SocketListener;
import com.zhangke.websocket.WebSocketHandler;
import com.zhangke.websocket.WebSocketManager;
import com.zhangke.websocket.WebSocketSetting;
import com.zhangke.websocket.response.ErrorResponse;

import org.java_websocket.framing.Framedata;

import java.nio.ByteBuffer;

public class Test1Activity extends AppCompatActivity {

    public SocketListener socketListener = new SimpleListener() {
        @Override
        public void onConnected() {
            super.onConnected();
            Log.e("AAA", "连接成功");
            WebSocketHandler.getDefault().send("{\"id\":\"2223\",\"instructions\":\"studentJoinRoom\",\"roomId\":\"room_855\"}");
        }

        @Override
        public void onConnectFailed(Throwable e) {
            super.onConnectFailed(e);
        }

        @Override
        public void onDisconnect() {
            super.onDisconnect();
        }

        @Override
        public <T> void onMessage(String message, T data) {
            super.onMessage(message, data);
        }

        @Override
        public void onSendDataError(ErrorResponse errorResponse) {
            super.onSendDataError(errorResponse);
        }

        @Override
        public <T> void onMessage(ByteBuffer bytes, T data) {
            super.onMessage(bytes, data);
        }

        @Override
        public void onPing(Framedata framedata) {
            super.onPing(framedata);
        }

        @Override
        public void onPong(Framedata framedata) {
            super.onPong(framedata);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);

        WebSocketSetting setting = new WebSocketSetting();
        //连接地址，必填，例如 wss://echo.websocket.org
        setting.setConnectUrl("ws://test-env2-live.train.newpage.ink/api/webSocket?ed22d6e624c94fe0b62cfa7e538eadb9");//必填

        //设置连接超时时间
        setting.setConnectTimeout(15 * 1000);

        //设置心跳间隔时间
        setting.setConnectionLostTimeout(30);

        //设置断开后的重连次数，可以设置的很大，不会有什么性能上的影响
        setting.setReconnectFrequency(10);

//        //设置Header
//        setting.setHttpHeaders(header);

        //设置消息分发器，接收到数据后先进入该类中处理，处理完再发送到下游
        //setting.setResponseProcessDispatcher(new AppResponseDispatcher());
        //接收到数据后是否放入子线程处理，只有设置了 ResponseProcessDispatcher 才有意义
        setting.setProcessDataOnBackground(true);

        //网络状态发生变化后是否重连，
        //需要调用 WebSocketHandler.registerNetworkChangedReceiver(context) 方法注册网络监听广播
        setting.setReconnectWithNetworkChanged(true);

        //通过 init 方法初始化默认的 WebSocketManager 对象
        WebSocketManager manager = WebSocketHandler.init(setting);
        //启动连接
        manager.start();

        //注意，需要在 AndroidManifest 中配置网络状态获取权限
        //注册网路连接状态变化广播
        WebSocketHandler.registerNetworkChangedReceiver(this);

        WebSocketHandler.getDefault().addListener(socketListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WebSocketHandler.getDefault().disConnect();
        WebSocketHandler.getDefault().destroy();
    }
}